<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\index\model\Commodity;
use app\index\model\Order;
use app\index\model\OrderGoods;
use app\index\model\Type;
use think\Exception;
use think\Request;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }
    
    public function getGameList(Request $request){
        try {
            $CommodityModel = new Commodity();
            $name = $request->get('name', null);
            if($name){
                $CommodityModel->where('title', 'like', "%{$name}%");
            }
            $CommodityList = $CommodityModel->where('status','1')
                ->whereNull('deletetime')
                ->select();
           
            return json(['data' => $CommodityList, 'code'=>1, 'msg'=>'获取列表成功']);
        }catch (\Exception $e){
            return json(['code'=>0, 'msg'=>'获取列表失败!']);
        }
        
    }
    
    public function getTypeList(Request $request){
        try {
            $TypeModel = new Type();
            $typeList = $TypeModel->where('status','1')
                ->whereNull('deletetime')
                ->select();
            return json(['data' => $typeList, 'code'=>1, 'msg'=>'获取列表成功']);
        }catch (\Exception $e){
            return json(['code'=>0, 'msg'=>'获取列表失败!']);
        }
    }
    
    public function addOrderGoods(Request $request){
        try {
            $ids = $request->post('ids', null);
            $type_id = $request->post('type_id', null);
            $username = $request->post('username', null);
            $remarks = $request->post('remarks', null);
            $phone = $request->post('phone', null);
            if(empty($type_id)){
                return json(['code'=>0, 'msg'=>'请选择内存卡大小!']);
            }
            if(empty($ids)){
                return json(['code'=>0, 'msg'=>'请选择您要安装的游戏!']);
            }
            $orderModel = new Order();
            $orderGoodsModel = new OrderGoods();
            $order_id = $orderModel->insertGetId([
                'type_id' => $type_id,
                'status' => 0,
                'username' => $username,
                'remarks' => $remarks,
                'phone' => $phone,
                'createtime' => time(),
                'updatetime' => time()
            ]);
            $goods = [];
            $ids = json_decode($ids);
            foreach ($ids as $k => $v){
                array_push($goods, [
                    'commodity_id' => $v,
                    'order_id' => $order_id,
                    'is_ok' => 0
                ]);
            }
            $res = $orderGoodsModel->insertAll($goods);
            if($res){
                return json(['code'=>1, 'msg'=>'提交成功!']);
            }
        }catch (\Exception $e){
            return json(['code'=>0, 'msg'=>'内部错误,请刷新画面重试!']);
        }
        
    }

}
